﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using service_desk_blred.Models;
using service_desk_blred.Utils;

namespace service_desk_blred.Controllers
{
    public class FornecedorController : Controller
    {

        public FornecedorController()
        {
            service = new FornecedorService();
        }

        FornecedorService service;

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Cadastrar()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Cadastrar(FornecedorModel model)
        {

            if (ModelState.IsValid)
            {

                ResponseInfo response = service.registrar(model);

                if (response.isSuccess())
                {
                    return RedirectToAction("Sucesso", "Fornecedor");

                }
                else
                {

                    ModelState.AddModelError("", "Erro ao cadastrar o fornecedor:  " + response.descricao);
                }
            }
            else
            {

                ModelState.AddModelError("", "Erro ao cadastrar o fornecedor");
            }

            return View();

        }

        public ActionResult Gerenciar()
        {

            ResponseInfo response = service.recuperarTodos();

            List<FornecedorModel> fornecedores = (List<FornecedorModel>)response.objResponse;

            return View(new ListaFornecedorModel(fornecedores));
        }

        public ActionResult Editar(int id)
        {

            ResponseInfo response = service.recuperarPorId(id);

            FornecedorModel model = (FornecedorModel)response.objResponse;

            return View(model);
        }

        [HttpPost]
        public ActionResult Editar(FornecedorModel model)
        {
            ResponseInfo response = service.update(model);
            if (response.isSuccess())
            {
                return RedirectToAction("Sucesso", "Fornecedor");
            }
            else
            {
                ModelState.AddModelError("", response.descricao);
                return View();
            }
        }


        public ActionResult Excluir(int id)
        {

            ResponseInfo response = service.recuperarPorId(id);

            FornecedorModel model = (FornecedorModel)response.objResponse;

            return View(model);
        }

        [HttpPost]
        public ActionResult Excluir(FornecedorModel model)
        {
            ResponseInfo response = service.excluir(model);
            if (response.isSuccess())
            {
                return RedirectToAction("Sucesso", "Fornecedor");
            }
            else
            {
                ModelState.AddModelError("", response.descricao);
                return View();
            }
        }


        public ActionResult Sucesso(String content)
        {
            ViewData["TitleMessage"] = "Operação realizada com Sucesso";
            return View();
        }

    }
}
